home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / pvm34b3.zip / pvm34b3 / pvm3 / examples / spmd.c < prev    next >
C/C++ Source or Header  |  1997-07-22  |  3KB  |  128 lines

  1.  
  2. static char rcsid[] =
  3.     "$Id: spmd.c,v 1.3 1997/07/09 13:25:19 pvmsrc Exp $";
  4.  
  5. /*
  6.  *         PVM version 3.4:  Parallel Virtual Machine System
  7.  *               University of Tennessee, Knoxville TN.
  8.  *           Oak Ridge National Laboratory, Oak Ridge TN.
  9.  *                   Emory University, Atlanta GA.
  10.  *      Authors:  J. J. Dongarra, G. E. Fagg, M. Fischer
  11.  *          G. A. Geist, J. A. Kohl, R. J. Manchek, P. Mucci,
  12.  *         P. M. Papadopoulos, S. L. Scott, and V. S. Sunderam
  13.  *                   (C) 1997 All Rights Reserved
  14.  *
  15.  *                              NOTICE
  16.  *
  17.  * Permission to use, copy, modify, and distribute this software and
  18.  * its documentation for any purpose and without fee is hereby granted
  19.  * provided that the above copyright notice appear in all copies and
  20.  * that both the copyright notice and this permission notice appear in
  21.  * supporting documentation.
  22.  *
  23.  * Neither the Institutions (Emory University, Oak Ridge National
  24.  * Laboratory, and University of Tennessee) nor the Authors make any
  25.  * representations about the suitability of this software for any
  26.  * purpose.  This software is provided ``as is'' without express or
  27.  * implied warranty.
  28.  *
  29.  * PVM version 3 was funded in part by the U.S. Department of Energy,
  30.  * the National Science Foundation and the State of Tennessee.
  31.  */
  32.  
  33. /*
  34. *    SPMD example using PVM 3
  35. *    Uses pvm_siblings() to determine number of tasks that were spawned
  36. *    together
  37. */
  38.  
  39. #include <stdio.h>
  40. #include <sys/types.h>
  41. #include "pvm3.h"
  42. #define MAXNPROC 32
  43.  
  44. main()
  45. {
  46.     int mytid;                  /* my task id */
  47.     int *tids;                  /* array of task ids */
  48.     int me;                     /* my process number */
  49.     int i;
  50.     int ntids;
  51.  
  52.     /* enroll in pvm */
  53.     mytid = pvm_mytid();
  54.  
  55.     /* determine the size of my sibling list */
  56.  
  57.     ntids = pvm_siblings(&tids);
  58.  
  59.     for (i = 0; i < ntids; i ++)
  60.         if ( tids[i] == mytid)
  61.         {
  62.             me = i;
  63.             break;
  64.         }
  65.         
  66.     if (me == 0)
  67.     {
  68.         printf("Pass a token through the %3d tid ring:\n", ntids);
  69.         for (i = 0; i < ntids; i ++)
  70.         {
  71.             printf( "%6d -> ", tids[i]);
  72.             if (i % 6 == 0 && i > 0)
  73.                 printf("\n");    
  74.         }
  75.         printf("%6d \n", tids[0]);
  76.     }
  77. /*--------------------------------------------------------------------------*/
  78.      
  79.      dowork( me, ntids, tids );
  80.  
  81.      /* program finished exit pvm */
  82.      pvm_exit();
  83.      exit(1);
  84. }
  85.  
  86. /* Simple example passes a token around a ring */
  87.  
  88. dowork( me, nproc, tids )
  89.      int me;
  90.      int nproc;
  91.      int tids[];
  92. {
  93.      int token;
  94.      int src, dest;
  95.      int count  = 1;
  96.      int stride = 1;
  97.      int msgtag = 4;
  98.  
  99.      /* Determine neighbors in the ring */
  100.      if ( me == 0 )
  101.         src = tids[nproc -1];
  102.      else
  103.          src = tids[me -1];
  104.  
  105.      if (me == nproc - 1)
  106.         dest = tids[0];
  107.      else
  108.         dest = tids[me + 1];
  109.  
  110.      if( me == 0 )
  111.      { 
  112.         token = dest;
  113.         pvm_initsend( PvmDataDefault );
  114.         pvm_pkint( &token, count, stride );
  115.         pvm_send( dest, msgtag );
  116.         pvm_recv( src, msgtag );
  117.         printf("token ring done\n");
  118.      }
  119.      else
  120.      {
  121.         pvm_recv( src, msgtag );
  122.         pvm_upkint( &token, count, stride );
  123.         pvm_initsend( PvmDataDefault );
  124.         pvm_pkint( &token, count, stride );
  125.         pvm_send( dest, msgtag );
  126.      }
  127. }
  128.